package com.ray.life;

import android.util.Log;

public class ContentParser {
    private final String TAG = "ContentParser";

    private final String ACT_INFO_PREFIX = "活動資訊：";
    private final String ACT_DATE_PREFIX = "活動時間：";
    private final String ACT_ADDR_PREFIX = "活動地點：";
    private final String END_OF_LINK = "</a><br>";

    private String mActDesc = null;
    private String mActInfo = null;
    private String mActDuration = null;
    private String mActAddr = null;

    public ContentParser(String content) {
        this.mActDesc = content;
    }

    public boolean parse() {
        try {
            int idxInfo = mActDesc.indexOf(ACT_INFO_PREFIX);
            int idxDate = mActDesc.indexOf(ACT_DATE_PREFIX);
            int idxAddr = mActDesc.indexOf(ACT_ADDR_PREFIX);

            if(idxInfo > -1) {
                /** to remove hyperlinks in desc */
                int inedx = mActDesc.indexOf(END_OF_LINK) + END_OF_LINK.length();
                if(idxDate > -1) {
                    mActInfo = mActDesc.substring(inedx, idxDate);
                } else {
                    mActInfo = mActDesc.substring(inedx);
                }
            }

            if(idxDate > -1) {
                if(idxAddr > -1) {
                    mActDuration = mActDesc.substring(idxDate + ACT_DATE_PREFIX.length(), idxAddr);
                } else {
                    mActDuration = mActDesc.substring(idxDate + ACT_DATE_PREFIX.length());
                }

                // example : 2010-08-31 00:00 ~ 2010-12-31 23:59<br>
                if (mActDuration.endsWith("<br>")) {
                    mActDuration = mActDuration.substring(0, mActDuration.length() - 4);
                }

                Log.v(TAG, "act duration : " + mActDuration);
            }

            if(idxAddr > -1) {
                mActAddr = mActDesc.substring(idxAddr + ACT_ADDR_PREFIX.length());
            }
        } catch(Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public String getActInfo() {
        return mActInfo == null ? "" : mActInfo;
    }

    public String getActDuration() {
        return mActDuration == null ? "" : mActDuration;
    }

    public String getActAddr() {
        return mActAddr == null ? "" : mActAddr;
    }
}
